package com.ihd.ihardware.view.tzc.discovery.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.content.Intent;
import android.databinding.ObservableArrayList;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.ihd.ihardware.R;
import com.ihd.ihardware.databinding.ItemCollectBinding;
import com.ihd.ihardware.databinding.ItemConcernBinding;
import com.ihd.ihardware.databinding.ItemDiscoveryBinding;
import com.ihd.ihardware.databinding.ItemDtBinding;
import com.ihd.ihardware.databinding.ItemMjBinding;
import com.ihd.ihardware.databinding.ItemZjBinding;
import com.ihd.ihardware.pojo.ArticalRes;
import com.ihd.ihardware.pojo.RecommendRes;
import com.ihd.ihardware.pojo.SceneRes;
import com.ihd.ihardware.pojo.SignsRes;
import com.ihd.ihardware.view.tzc.discovery.model.DiscoveryRepository;
import com.ihd.ihardware.view.tzc.discovery.view.BaseWebActivity;
import com.ihd.ihardware.view.tzc.discovery.view.CommentsActivity;
import com.ihd.ihardware.view.tzc.discovery.view.WebActivity;
import com.ihd.ihardware.view.tzc.me.view.MyPageActivity;

import java.util.List;

import cn.wowjoy.commonlibrary.adapter.CommonAdapter;
import cn.wowjoy.commonlibrary.utils.DensityUtil;
import cn.wowjoy.commonlibrary.utils.GlideRoundTransform;

public class DiscoveryViewModel extends AndroidViewModel {

    private DiscoveryRepository mDiscoveryRepository;

    public DiscoveryViewModel(@NonNull Application application) {
        super(application);
    }

    private ObservableArrayList<RecommendRes.DataBean> mTestDetails = new ObservableArrayList<>();
    public CommonAdapter mCommonAdapter = new CommonAdapter<RecommendRes.DataBean, ItemConcernBinding>(R.layout.item_concern, mTestDetails, null) {
        @Override
        public void convert(ItemConcernBinding mDataBinding, RecommendRes.DataBean test_concern, int position) {
            if (!TextUtils.isEmpty(test_concern.getAvatar()))
                Glide.with(mDataBinding.concernItemLL.getContext())
                        .load(Uri.parse(test_concern.getAvatar()))
                        .apply(RequestOptions.circleCropTransform())
                        .into(mDataBinding.head);
            if (test_concern.isConcern()) {
                mDataBinding.concernBtn.setBackgroundResource(R.drawable.corners_5661f3);
                mDataBinding.concernIV.setVisibility(View.GONE);
                mDataBinding.concernTV.setText("已关注");
                mDataBinding.concernTV.setTextColor(mDataBinding.concernTV.getContext().getResources().getColor(R.color.C_FFFFFF));
            } else {
                mDataBinding.concernBtn.setBackgroundResource(R.drawable.corners_line_bg);
                mDataBinding.concernIV.setVisibility(View.VISIBLE);
                mDataBinding.concernTV.setText("关注");
                mDataBinding.concernTV.setTextColor(mDataBinding.concernTV.getContext().getResources().getColor(R.color.C_5661F3));
            }
            mDataBinding.concernBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (test_concern.isConcern())
                        mDiscoveryRepository.delConcern(test_concern.getUserId());
                    else
                        mDiscoveryRepository.concern(test_concern.getUserId());
                }
            });

            mDataBinding.head.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(v.getContext(), MyPageActivity.class);
                    i.putExtra("userId", test_concern.getUserId());
                    v.getContext().startActivity(i);
                }
            });
        }
    };

    public void getConcernList() {
        if (mDiscoveryRepository == null)
            mDiscoveryRepository = new DiscoveryRepository();
        mDiscoveryRepository.getConcernList();
    }

    public void setConcernList(List<RecommendRes.DataBean> list) {
        mTestDetails.clear();
        if (list != null && list.size() > 0)
            mTestDetails.addAll(list);
    }


    private ObservableArrayList<SignsRes.DataBean.SignDetailsBean> mConcerned = new ObservableArrayList<>();
    public CommonAdapter mConcernedAdapter = new CommonAdapter<SignsRes.DataBean.SignDetailsBean, ItemDiscoveryBinding>(R.layout.item_discovery, mConcerned, new CommonAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View v, int position) {
            Intent i = new Intent(v.getContext(), CommentsActivity.class);
            i.putExtra("signsBean", mConcerned.get(position));
            v.getContext().startActivity(i);
        }
    }) {
        @Override
        public void convert(ItemDiscoveryBinding mDataBinding, SignsRes.DataBean.SignDetailsBean test_concern, int position) {
            if (!TextUtils.isEmpty(test_concern.getAvatar()))
                Glide.with(mDataBinding.concernedItemLL.getContext())
                        .load(Uri.parse(test_concern.getAvatar()))
                        .apply(RequestOptions.circleCropTransform())
                        .into(mDataBinding.head);

            Glide.with(mDataBinding.concernedItemLL.getContext())
                    .load(Uri.parse((test_concern.getOssFiles() != null && test_concern.getOssFiles().size() > 0) ? test_concern.getOssFiles().get(0) : ""))
                    .into(mDataBinding.bg);

            mDataBinding.head.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(v.getContext(), MyPageActivity.class);
                    i.putExtra("userId", test_concern.getUserId());
                    v.getContext().startActivity(i);
                }
            });

            mDataBinding.tag.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (test_concern.isTaged())
                        mDiscoveryRepository.delTag(test_concern.getSignId());
                    else
                        mDiscoveryRepository.tag(test_concern.getSignId());
                }
            });
        }
    };

    public void getDiscoveryList(int pageNum, int pageSize) {
        if (mDiscoveryRepository == null)
            mDiscoveryRepository = new DiscoveryRepository();
        mDiscoveryRepository.getDiscoveryList(pageNum, pageSize);
    }

    public void setDiscoveryList(List<SignsRes.DataBean.SignDetailsBean> list) {
        mConcerned.clear();
        if (list != null && list.size() > 0)
            mConcerned.addAll(list);
    }

    private ObservableArrayList<SceneRes.DataBean> mZJ = new ObservableArrayList<>();
    public CommonAdapter mZJAdapter = new CommonAdapter<SceneRes.DataBean, ItemZjBinding>(R.layout.item_zj, mZJ, new CommonAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View v, int position) {
            if ("1".equals(mZJ.get(position).getType())) {
                Intent i = new Intent(v.getContext(), BaseWebActivity.class);
                i.putExtra("url", mZJ.get(position).getUrl());
                v.getContext().startActivity(i);
            } else {
                Intent i = new Intent(v.getContext(), WebActivity.class);
                i.putExtra("title", mZJ.get(position).getCategoryName() + "专家有话说");
                i.putExtra("url", mZJ.get(position).getId());
                i.putExtra("count", mZJ.get(position).getCommentNum());
                i.putExtra("collectState", mZJ.get(position).isCollectState());
                v.getContext().startActivity(i);
            }

        }
    }) {
        @Override
        public void convert(ItemZjBinding mDataBinding, SceneRes.DataBean test_concern, int position) {
            Glide.with(mDataBinding.concernItemLL.getContext())
                    .load(Uri.parse(test_concern.getIconUrl()))
                    .apply(new RequestOptions().transform(new GlideRoundTransform(5)))
                    .into(mDataBinding.bg);
        }
    };

    public void getZJList() {
        if (mDiscoveryRepository == null)
            mDiscoveryRepository = new DiscoveryRepository();
        mDiscoveryRepository.getZJList();
    }

    public void setZJList(List<SceneRes.DataBean> list) {
        mZJ.clear();
        if (list != null && list.size() > 0)
            mZJ.addAll(list);
    }

    private ObservableArrayList<SceneRes.DataBean> mMJ = new ObservableArrayList<>();
    public CommonAdapter mMJAdapter = new CommonAdapter<SceneRes.DataBean, ItemMjBinding>(R.layout.item_mj, mMJ, new CommonAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View v, int position) {
            if ("1".equals(mMJ.get(position).getType())) {
                Intent i = new Intent(v.getContext(), BaseWebActivity.class);
                i.putExtra("url", mMJ.get(position).getUrl());
                v.getContext().startActivity(i);
            } else {
                Intent i = new Intent(v.getContext(), WebActivity.class);
                i.putExtra("title", mMJ.get(position).getCategoryName() + "秘籍-详情");
                i.putExtra("url", mMJ.get(position).getId());
                i.putExtra("count", mMJ.get(position).getCommentNum());
                i.putExtra("collectState", mMJ.get(position).isCollectState());
                v.getContext().startActivity(i);
            }
        }
    }) {
        @Override
        public void convert(ItemMjBinding mDataBinding, SceneRes.DataBean test_concern, int position) {
            ViewGroup.LayoutParams layoutParams = mDataBinding.bg.getLayoutParams();
            layoutParams.width = DensityUtil.getWidthPixels() - DensityUtil.dip2px(24);
            layoutParams.height = (int) (layoutParams.width / 2.5f);
            mDataBinding.bg.setLayoutParams(layoutParams);
            Glide.with(mDataBinding.concernItemLL.getContext())
                    .load(Uri.parse(test_concern.getIconUrl()))
                    .apply(new RequestOptions().transform(new GlideRoundTransform(5)))
                    .into(mDataBinding.bg);
        }
    };

    public void getMJList() {
        if (mDiscoveryRepository == null)
            mDiscoveryRepository = new DiscoveryRepository();
        mDiscoveryRepository.getMJList();
    }

    public void setMJList(List<SceneRes.DataBean> list) {
        mMJ.clear();
        if (list != null && list.size() > 0)
            mMJ.addAll(list);
    }

    public void getJXMJList() {
        if (mDiscoveryRepository == null)
            mDiscoveryRepository = new DiscoveryRepository();
        mDiscoveryRepository.getJXMJList();
    }

    private ObservableArrayList<SignsRes.DataBean.SignDetailsBean> mDT = new ObservableArrayList<>();
    public CommonAdapter mDTAdapter = new CommonAdapter<SignsRes.DataBean.SignDetailsBean, ItemDtBinding>(R.layout.item_dt, mDT, new CommonAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View v, int position) {
            Intent i = new Intent(v.getContext(), CommentsActivity.class);
            i.putExtra("signsBean", mDT.get(position));
            v.getContext().startActivity(i);
        }
    }) {
        @Override
        public void convert(ItemDtBinding mDataBinding, SignsRes.DataBean.SignDetailsBean test_concern, int position) {
            RequestOptions headro = RequestOptions.circleCropTransform();
            Glide.with(mDataBinding.concernItemLL.getContext())
                    .load(Uri.parse((test_concern.getAvatar() != null) ? test_concern.getAvatar() : ""))
                    .apply(headro)
                    .into(mDataBinding.head);

            ViewGroup.LayoutParams layoutParams = mDataBinding.bg.getLayoutParams();
            layoutParams.width = (DensityUtil.getWidthPixels() - DensityUtil.dip2px(36)) / 2;
            layoutParams.height = (int) (layoutParams.width * 1.35f);
            mDataBinding.bg.setLayoutParams(layoutParams);
            RequestOptions ro = new RequestOptions()
                    .transform(new GlideRoundTransform(5));
            Glide.with(mDataBinding.concernItemLL.getContext())
                    .load(Uri.parse((test_concern.getOssFiles() != null && test_concern.getOssFiles().size() > 0) ? test_concern.getOssFiles().get(0) : ""))
                    .apply(ro)
                    .into(mDataBinding.bg);

            mDataBinding.tag.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (test_concern.isTaged())
                        mDiscoveryRepository.delTag(test_concern.getSignId());
                    else
                        mDiscoveryRepository.tag(test_concern.getSignId());
                }
            });

            mDataBinding.head.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(v.getContext(), MyPageActivity.class);
                    i.putExtra("userId", test_concern.getUserId());
                    v.getContext().startActivity(i);
                }
            });
        }
    };

    public void getDTList(int pageNum, int pageSize) {
        if (mDiscoveryRepository == null)
            mDiscoveryRepository = new DiscoveryRepository();
        mDiscoveryRepository.getDTList(pageNum, pageSize);
    }

    public void setDTList(List<SignsRes.DataBean.SignDetailsBean> list) {
        mDT.clear();
        if (list != null && list.size() > 0)
            mDT.addAll(list);
    }

    private ObservableArrayList<ArticalRes.DataBean.ListBean> mCollect = new ObservableArrayList<>();
    public CommonAdapter mCollectAdapter = new CommonAdapter<ArticalRes.DataBean.ListBean, ItemCollectBinding>(R.layout.item_collect, mCollect, new CommonAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View v, int position) {
            if ("1".equals(mCollect.get(position).getType())) {
                Intent i = new Intent(v.getContext(), BaseWebActivity.class);
                i.putExtra("url", mCollect.get(position).getUrl());
                v.getContext().startActivity(i);
            } else {
                Intent i = new Intent(v.getContext(), WebActivity.class);
                i.putExtra("title", mCollect.get(position).getCategoryName() + "秘籍-详情");
                i.putExtra("url", mCollect.get(position).getId());
                i.putExtra("count", mCollect.get(position).getCommentNum());
                i.putExtra("collectState", mCollect.get(position).isCollectState());
                v.getContext().startActivity(i);
            }
        }
    }) {
        @Override
        public void convert(ItemCollectBinding mDataBinding, ArticalRes.DataBean.ListBean test_collect, int position) {
            RequestOptions requestOptions = new RequestOptions().transform(new GlideRoundTransform(5));
            Glide.with(mDataBinding.concernedItemLL.getContext())
                    .load(Uri.parse(test_collect.getIconUrl() != null ? test_collect.getIconUrl() : ""))
                    .apply(requestOptions)
                    .into(mDataBinding.head);
        }
    };

    public void getCollectList(int pageNum, int pageSize, String category) {
        if (mDiscoveryRepository == null)
            mDiscoveryRepository = new DiscoveryRepository();
        mDiscoveryRepository.getCollectList(pageNum, pageSize, category);
    }

    public void setCollectList(List<ArticalRes.DataBean.ListBean> list) {
        mCollect.clear();
        if (list != null && list.size() > 0)
            mCollect.addAll(list);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (null != mDiscoveryRepository) {
            mDiscoveryRepository.onDestroy();
            mDiscoveryRepository = null;
        }
    }
}
